package service;

import dao.ListDao;
import entity.Article;
import entity.Dept;
import entity.PageEntity;
import entity.User;

import javax.servlet.ServletException;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class ListService {
    ListDao listDao = new ListDao();

    /**
     * 查询所有用户信息
     */
    public List<User> findAll(User user) {
        return listDao.findAll(user);

    }

    /**
     * 根据搜索信息查找对应的数据
     *
     * @throws ServletException
     * @throws IOException
     */
    public List<User> searchValue(String searchVal) {
        return listDao.searchValue(searchVal);
    }

    /**
     * 根据分页查询信息
     *
     * @param currPage
     * @param realName
     * @return
     */
    public PageEntity<User> findByPage(Integer currPage, String realName) {
        PageEntity<User> pageEntity = new PageEntity();

        //结束页面
        Integer pageSize = pageEntity.getPageSize();

        //开始页面
        Integer startPage = (currPage - 1) * pageSize;

        //根据搜索查找并且分页
        List<User> userList = listDao.findByPage(realName, pageSize, startPage);

        //计算count值
        Integer count = listDao.countByUser(realName);

        //总页数
        Integer totalPage = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;

        pageEntity.setCurrPage(currPage);
        pageEntity.setData(userList);
        pageEntity.setTotalCount(count);
        pageEntity.setTotalPage(totalPage);
        return pageEntity;
    }

    /**
     * 查询所有部门
     *
     * @return
     */
    public List<Map<String, Object>> findDepartments() {
        return listDao.findDepartments();
    }

    /**
     * 查询部门对应员工
     *
     * @param deptId
     * @return
     */
    public List<User> findByUser(String deptId) {
        return listDao.findByUser(deptId);
    }

    /**
     * 查询账户的关注数量
     *
     * @param loginUserId
     * @return
     */
    public Integer myself(Long loginUserId) {
        return listDao.myself(loginUserId);
    }

    /**
     * 查看用户的详细信息
     *
     * @param userID
     * @return
     */
    public User findByUserDetail(Long userID) {
        return listDao.findByUserDetail(userID);
    }

    /**
     * 查看用户的关注数
     *
     * @param userID
     * @return
     */
    public Integer countUser(Long userID) {
        return listDao.countUser(userID);
    }

    /**
     * 被看数加1
     *
     * @param userID
     */
    public void addLock(Long userID) {
        listDao.addLock(userID);
    }

    /**
     * 查询所属部门
     *
     * @param
     * @return
     */
    public List<Dept> findByDept() {
        return listDao.findByDept();
    }

    /**
     * 更新数据
     */
    public void update(User user) {
        listDao.update(user);
    }

    /**
     * 修改pic
     *
     * @param user
     */
    public void uploadPic(User user) {
        listDao.uploadPic(user);
    }

    /**
     * 查询登录用户的关注信息
     *
     * @param id
     * @return
     */
    public List<Long> findByFocus(Long id) {
        return listDao.findByFocus(id);

    }

    /**
     * 查询关注的用户
     *
     * @return
     */
    public PageEntity<User> findPageFocus(Integer currPage, Long userID) {
        PageEntity<User> entity = new PageEntity<>();

        //最大页数
        Integer pageSize = entity.getPageSize();

        //开始页面
        Integer startPage = (currPage - 1) * pageSize;

        //查找分页和信息
        List<User> userList = listDao.findPageFocusPage(startPage, userID, pageSize);

        //计算总页数
        Integer count = listDao.findByCount(userID);

        //总页数
        Integer totalPage = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;

        entity.setCurrPage(currPage);
        entity.setData(userList);
        entity.setTotalCount(count);
        entity.setTotalPage(totalPage);
        return entity;
    }

    /**
     * 查看有无这条数据
     *
     * @param userId
     * @param id
     * @return
     */
    public Integer isFocus(String userId, Long id) {
        return listDao.isFocus(userId, id);
    }

    /**
     * 删除这条数据
     *
     * @param userId
     * @param id
     */
    public void removeFocus(String userId, Long id) {
        listDao.removeFoucs(userId, id);
    }

    /**
     * 添加数据
     *
     * @param userId
     * @param id
     */
    public void addFocus(String userId, Long id) {
        listDao.addFocus(userId, id);
    }

    /**
     * 所有文章＋分页
     *
     * @return
     */
    public PageEntity<Article> findPageAllArticle(Integer currPage) {
        PageEntity<Article> entity = new PageEntity<>();

        //最大页数
        Integer pageSize = entity.getPageSize();

        //开始页面
        Integer startPage = (currPage - 1) * pageSize;

        //查找分页和信息
        List<Article> articleList = listDao.findPageAllArticlePage(startPage, pageSize);

        //计算总页数
        Integer count = listDao.findByArticleCount();

        //总页数
        Integer totalPage = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;

        entity.setCurrPage(currPage);
        entity.setData(articleList);
        entity.setTotalCount(count);
        entity.setTotalPage(totalPage);
        return entity;
    }

    /**
     * 搜索文章信息
     *
     * @param currPage
     * @param articleName
     * @return
     */
    public PageEntity<Article> findPageLikeArticle(Integer currPage, String articleName) {
        PageEntity<Article> entity = new PageEntity<>();

        //最大页数
        Integer pageSize = entity.getPageSize();

        //开始页面
        Integer startPage = (currPage - 1) * pageSize;

        //查找分页和信息
        List<Article> likeArticleList = listDao.findPageLikeArticle(startPage, pageSize, articleName);

        //计算总页数
        Integer count = listDao.findPageLikeArticleCount(articleName);

        //总页数
        Integer totalPage = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;

        entity.setCurrPage(currPage);
        entity.setData(likeArticleList);
        entity.setTotalCount(count);
        entity.setTotalPage(totalPage);
        return entity;
    }

    /**
     * 插入文章
     */
    public void addArticle(Article article) {
        listDao.addArticle(article);
    }

    /**
     * 收藏文章＋分页
     *
     * @param userId
     * @param currPage
     * @return
     */
    public PageEntity<Article> SaveArticle(Long userId, Integer currPage, String title) {
        PageEntity<Article> entity = new PageEntity<>();

        //最大页数
        Integer pageSize = entity.getPageSize();

        //开始页面
        Integer startPage = (currPage - 1) * pageSize;

        //查找分页和信息
        List<Article> saveArticle = listDao.findPageSaveArticle(startPage, pageSize, userId, title);

        //计算总页数
        Integer count = listDao.findPageSaveArticleCount(userId, title);

        //总页数
        Integer totalPage = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;

        entity.setCurrPage(currPage);
        entity.setData(saveArticle);
        entity.setTotalCount(count);
        entity.setTotalPage(totalPage);
        return entity;
    }

    /**
     * 查找文章信息
     *
     * @param articleId
     * @return
     */
    public Article findByArticleInfo(Long articleId) {
        return listDao.findByArticleInfo(articleId);
    }

    /**
     * 是否收藏这篇文章
     *
     * @param articleId
     * @param id
     * @return
     */
    public Integer saveArticle(Long articleId, Long id) {
        return listDao.saveArticle(articleId, id);
    }

    /**
     * 浏览量＋1
     *
     * @param articleId
     */
    public void addBrowseCount(Long articleId) {
        listDao.addBrowseCount(articleId);
    }

    /**
     * 收藏次数
     * @param articleId
     * @return
     */
    public Integer saveArticleCount(Long articleId) {
        return listDao.saveArticleCount(articleId);
    }

    /**
     * 我关注的人里关注这篇文章的人
     * @param articleId
     * @param id
     * @return
     */
    public List<User> addAndSave(Long articleId, Long id) {
        return listDao.addAndSave(articleId,id);
    }

    /**
     * 取消收藏
     * @param articleId
     * @param id
     */
    public void deleteArticle(Long articleId, Long id) {
        listDao.deleteArticle(articleId,id);
    }

    /**
     * 收藏文章
     * @param articleId
     * @param id
     */
    public void add(Long articleId, Long id) {
        listDao.add(articleId,id);
    }
}
